﻿using SetSailCommon;
using SetSailDal;
using SetSailModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SetSailView
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public partial class Frm_YHGL : UserControl
    {
        public Frm_YHGL()
        {
            InitializeComponent();
        }
        private int pageIndex = 0;
        private void Frm_YHGL_Load(object sender, EventArgs e)
        {
            this.init(); //初始化
        }

        private void init()
        {
            //设置每页条目
            this.ucPagerControl31.PageSize = Convert.ToInt32(CookieData.getInstance().ZDGL.Where(a => a.MXDM == "PAGECOUNT").FirstOrDefault().MXZ);
            //禁用权限操作
            if (CookieData.getInstance().YHXX.ZHLX != "sys")
                this.dgv_yhxx.Columns[2].ReadOnly = true;
            //登录状态
            List<KeyValuePair<string, string>> lscbm_kflx = new List<KeyValuePair<string, string>>();

            lscbm_kflx.Add(new KeyValuePair<string, string>("", "全部"));
            lscbm_kflx.Add(new KeyValuePair<string, string>("正常", "正常"));
            lscbm_kflx.Add(new KeyValuePair<string, string>("禁止", "禁止"));
            cob_dlzt.Source = lscbm_kflx;
            cob_dlzt.SelectedValue = "";

            //获取列表
            var totalCount = 0;
            this.LoadList(out totalCount);
            //设置分页
            this.setPage(totalCount);
        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            //筛选条件
            pageIndex = 0; //清空index，防止数据变更触发分页事件
            this.ucPagerControl31.FirstPage(); //查询后跳转第一条

            var totalCount = 0;
            this.LoadList(out totalCount, getwhere());
            //设置分页 查询后数据变更需要重新设置分页
            this.setPage(totalCount);
        }

        /// <summary>
        /// 获取并绑定列表
        /// </summary>
        private void LoadList(out int totalCount, string where = "")
        {

            ResultModel<List<QH_YHXX>> result = YHGLDal.GetPageList(this.ucPagerControl31.PageIndex, this.ucPagerControl31.PageSize, where);
            if (result.Code == ResultCode.Success)
            {
                totalCount = result.Count;
                this.dgv_yhxx.AutoGenerateColumns = false;
                this.dgv_yhxx.DataSource = result.Data;
            }
            else
            {
                totalCount = 0;
                MessShow.ShowMistake(result.Message);
            }
        }

        private string getwhere()
        {
            var dlzh = txt_Name.InputText;
            var dlzt = cob_dlzt.SelectedValue;
            var where = string.Empty;
            if (dlzh.Length > 0)
            {
                where += $" DLZH like '%{dlzh}%'";
            }
            if (dlzt.Length > 0)
            {
                where += where.Length > 0 ? " AND " : "";
                where += $" DLZT = '{dlzt}'";
            }
            return where;
        }

        private void setPage(int count)
        {
            List<object> lstPage2 = new List<object>();
            for (int i = 0; i < count; i++)
            {
                lstPage2.Add(i);
            }
            ucPagerControl31.DataSource = lstPage2;
        }

        private void dgv_yhgl_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var column = this.dgv_yhxx.Columns[e.ColumnIndex].DataPropertyName;
            if (column == "JZDL" || column == "HFDL")
            {
                var dlzt = this.dgv_yhxx.Rows[e.RowIndex].Cells[5].Value.ToString();
                if (dlzt == "正常" && column == "HFDL")
                {
                    MessShow.ShowHint("用户已是正常状态");
                    return;
                }
                if (dlzt == "禁止" && column == "JZDL")
                {
                    MessShow.ShowHint("用户已是禁止状态");
                    return;
                }
                var dlzh = this.dgv_yhxx.Rows[e.RowIndex].Cells[1].Value.ToString() + ",";
                var msg = "是否恢复登录";
                var shzt = "正常";
                if (column == "JZDL")
                {
                    shzt = "禁止";
                    msg = "是否禁止登录";
                }
                DialogResult dialog = MessShow.ShowQuery("操作账户：" + dlzh + msg);
                if (dialog == DialogResult.OK)
                {
                    var result = YHGLDal.EditDLZT(this.dgv_yhxx.Rows[e.RowIndex].Cells[0].Value.ToString(), shzt);
                    if (result.Code == ResultCode.Success)
                    {
                        LoadList(out int count, getwhere());
                        MessShow.ShowSure("保存成功");
                    }
                    else
                    {
                        MessShow.ShowHint(result.Message);
                    }
                }
            }
        }

        /// <summary>
        /// 分页控件 数据操作事件
        /// </summary>
        /// <param name="currentSource"></param>
        private void ucPagerControl31_ShowSourceChanged(object currentSource)
        {
            if (currentSource != null)
            {
                var currindex = (Json.ToObject<int[]>(currentSource.ToJson()))[0];
                if (pageIndex != currindex)
                {
                    int count = 0;
                    LoadList(out count, getwhere());
                    pageIndex = currindex;
                }

            }
        }

        ComboBox cbo = new ComboBox();
        string cmbtype = "";
        string cmbid = "";
        private void dgv_yhxx_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            cbo.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
            if (dgv_yhxx.CurrentCell.OwningColumn.DataPropertyName == "ZHLX")
            {
                cmbtype = dgv_yhxx.CurrentCell.OwningColumn.DataPropertyName;
                cbo = e.Control as ComboBox; //保存当前的事件源。为了触发事件后。在取消
                cmbtype = cbo.Text;
                cmbid = dgv_yhxx.Rows[dgv_yhxx.CurrentRow.Index].Cells[0].Value.ToString();

                cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged);
            }
        }

        void cbo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            var zhlx = combox.Text;
            if (cmbtype == zhlx || cmbid != dgv_yhxx.Rows[dgv_yhxx.CurrentRow.Index].Cells[0].Value.ToString())
            {
                return;
            }
            var result = YHGLDal.EditZHLX(dgv_yhxx.Rows[dgv_yhxx.CurrentRow.Index].Cells[0].Value.ToString(), combox.Text);
            if (result.Code == ResultCode.Success)
            {
                cmbtype = zhlx;
                MessShow.ShowSure(result.Message);
            }
            else
            {
                MessShow.ShowHint(result.Message);
            }
            //combox.Text和cbo.Text获取的值是相同的

            //做完处理，须撤销动态事件。如果不撤销会遇到什么问题。你可以自己试试
            //cbo.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
            //或者
            //combox.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
        }

        private void ucBtnExt3_BtnClick(object sender, EventArgs e)
        {
            var ID = this.dgv_yhxx.SelectedRows[0].Cells[0].Value.ToString();
            if (ID.Length > 0)
            {
                DialogResult dialog = MessShow.ShowQuery("操作账户：" + this.dgv_yhxx.SelectedRows[0].Cells[1].Value.ToString() + "，是否初始化密码");
                if (dialog == DialogResult.OK)
                {
                    var res = YHGLDal.ResetPWD(ID);
                    MessShow.ShowHint(res.Message);
                }
            }
            else
            {
                MessShow.ShowSure("请选择需要初始化的用户");
            }
        }
    }
}
